<template>
  <!-- 接收参考单明细 -->
  <div>
    <FormPanel :list="formList" :isSubmitBtn="false" labelWidth="0"></FormPanel>
    <FilterTable
      ref="table"
      :dataSource="tableDataList"
      :isMemoryPagination="true"
      :columns="columns"
      height="auto"
      maxHeight="350"
      columnsRef="partPartPurchasePurchaseOrderCreateReceiveBillDetail"
      :isSelectColumn="billTypeId == 97031002 ? true : false"
      :uidkey="billTypeId == 97031002 ? 'shortId' : 'uid'"
      :defaultSelections="defaultSelectRow"
      :onRowSelectChange="handleRowSelect"
      :onColumnsChange="columns => (this.columns = columns)"
      :onPageChange="handleTablePageChange"
    >
      <template slot="controls">
        <!-- <el-button size="small" type="primary">导出</el-button>
        <el-button size="small" type="primary">打印</el-button>-->
      </template>
    </FilterTable>
    <div class="btnWrap">
      <el-button @click="handleCancle">取消</el-button>
      <el-button type="primary" @click="handleMergeBtnClick">合并计划数量</el-button>
    </div>
    <!-- 合并结果弹窗 -->
    <BaseDialog :visible.sync="mergeResultVisible" title="合并明细" :destroyOnClose="true" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <mergeBillPartResult :partList="coalescentPartList" @close="mergeResultVisible = false" @onConfirm="handleCoalescentConfirm"></mergeBillPartResult>
    </BaseDialog>
  </div>
</template>

<script>
import mergeBillPartResult from './mergeBillPartResult';
import { notifyAction } from '@/utils';

export default {
  components: {
    mergeBillPartResult
  },
  props: {
    billTypeId: Number,
    dataList: Array
  },
  data() {
    return {
      formList: this.createFormList(),
      columns: this.createTableColumns(),
      mergeResultVisible: false,
      tableDataList: [], // table数据列表
      coalescentPartList: [], // 合并后的配件列表
      page: 1, // 当前页码
      selectedPageRowObj: {}, // 页面选中行项对象
      defaultSelectRow: [], // 默认选中的行项
      selectRowData: [] // 选中的table行项
    };
  },
  created() {
    this.tableDataList = this.dataList;
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'RADIO',
          fieldName: 'a1',
          initialValue: 1,
          itemList: [
            { text: '合并配件数量到相同配件', value: 1 }
            // { text: '合并替换组计划数量', value: 2 }
          ]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '参考单据编号',
          dataIndex: 'mainNo',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          width: 150,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          width: 150,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '计划数量',
          dataIndex: 'nOrderQty',
          filter: true,
          filterType: 'number'
        },
        // {
        //   title: '厂家配件代码',
        //   dataIndex: 'a5'
        // },
        {
          title: '主组号',
          dataIndex: 'mainGroupNo',
          filter: true,
          filterType: 'number'
        }
      ];
    },
    // 点击数量合并
    handleMergeBtnClick() {
      // 欠料单分类下需要走选择合并
      if (this.billTypeId == 97031002) {
        // let partList = [];
        // for (let item in this.selectedPageRowObj) {
        //   partList = partList.concat(this.selectedPageRowObj[item] || []);
        // }
        if (this.selectRowData) {
          this.mergePartList(this.selectRowData);
        } else {
          notifyAction('请选择需要合并的配件', 'warning');
        }
      } else {
        this.mergePartList(this.tableDataList);
      }
    },
    // 合并配件并打开配件合并弹窗
    mergePartList(list) {
      if (list.length) {
        let obj = {};
        list.map(item => {
          obj[item.partNo] ? (item.shortId ? (obj[item.partNo].shortId += ',' + item.shortId) : '') : '';
          obj[item.partNo] ? (obj[item.partNo].nOrderQty += item.nOrderQty) : (obj[item.partNo] = item);
        });
        let partArr = [];
        for (let item in obj) {
          obj[item].shortPartId = obj[item].shortId;
          partArr.push(obj[item]);
        }
        console.log(partArr, '合并后的配件列表');
        this.coalescentPartList = partArr;
        this.mergeResultVisible = true;
      }
    },
    // 合并配件数量点击确定
    handleCoalescentConfirm(e) {
      this.$emit('coalescentConfirm', e);
      this.handleCancle();
    },
    handleCancle() {
      this.$emit('close');
    },
    // 页码变化
    handleTablePageChange(e) {
      // console.log(e);
      this.page = e.pageNum;
      // this.defaultSelectRow = this.selectedPageRowObj[this.page] || [];
    },
    // 选中行项变化
    handleRowSelect(e) {
      console.log(e, 'table选中行项变化');
      this.selectRowData = e;
      // this.selectedPageRowObj[this.page] = e;
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
